package com.huike.task.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class TestTask {

    //方式一
    public void task01() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程");
            }
        }).start();


        //方式二
        new Thread() {
            @Override
            public void run() {
                System.out.println("子线程");
            }
        }.start();

        //方式三 线程池
        ThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(10);
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
        System.out.println("主线程");
    }

    //方式四 CompletableFuture
    public static void main(String[] args) {

        CompletableFuture<String> task1= CompletableFuture.supplyAsync(()->{
            // 游客1
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线1程执行了");
            return "1";
        });

        CompletableFuture<Integer> task2= CompletableFuture.supplyAsync(()->{
            // 游客2
            System.out.println("子线2程执行了");
            return 2;
        });
        // 等待子线程 方式1
        //  CompletableFuture.allOf(task1,task2).join();
        // 等待方式2
        try {
            String s = task1.get();
            Integer i =task2.get();
            System.out.println("s:"+s);
            System.out.println("i:"+i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("主线程执行了...");
    }

}
